/**
 * Created by xiewei on 11/13/15.
 * nav导航效果 页面切换效果
 */
/**
 * 判断是否为移动设备
 */
var ifMobile = function(){
    var browser={
        versions:function(){
            var u = navigator.userAgent;
            return {//移动终端浏览器版本信息
                trident: u.indexOf('Trident') > -1, //IE内核
                presto: u.indexOf('Presto') > -1, //opera内核
                webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
                gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
                mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
                ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
                android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器
                iPhone: u.indexOf('iPhone') > -1 , //是否为iPhone或者QQHD浏览器
                iPad: u.indexOf('iPad') > -1, //是否iPad
                webApp: u.indexOf('Safari') == -1 //是否web应该程序，没有头部与底部
            };
        }(),
        language:(navigator.browserLanguage || navigator.language).toLowerCase()
    }
    if(browser.versions.mobile || browser.versions.ios || browser.versions.android ||
        browser.versions.iPhone || browser.versions.iPad){
        return true;
    }
    return false;
}
var webSiteVerShift = function(){
    var href = location.href;
    var curVer = href.indexOf('mobile');//当前版本
    if(ifMobile()){
        if(curVer < 0){
            //现在为PC端
            href = href.split('/');
            window.open('mobile/'+href[href.length-1],'_self');
        }
    }else{
        if(curVer > 0){
            //当前为移动端
            href = href.split('/');
            window.open('../'+href[href.length-1],'_self');
        }
    }
}
/**
 * define
 */
var imgDir = 'img/';
var topBanner = $('#top-banner');
var ifNavbarChange = true;//头部导航变化
var mainContent = $('.main-content');
var detailScroll = $('.detail div.scroll');
var detailBigImg = $('.detail span.bigImg');
//从下、右、左侧渐进
var fromBottom = $('#fromBottom');
var fromLeft = $('#fromLeft');
var fromRight = $('#fromRight');
/**
 * 获取屏幕宽度
 * @returns {Number}
 */
var getPageWidth = function(){
    return document.documentElement.clientWidth ? document.documentElement.clientWidth:document.body.clientWidth;
}
/**
 * 获取屏幕宽高度
 * @returns {Number}
 */
var getPageHeight = function(){
    return document.documentElement.clientHeight ? document.documentElement.clientHeight:document.body.clientHeight;
}
/**
 * 视频重新开始播放
 */
var start = function(video){
    video.currentTime = 0;
    video.play();
}
/**
 * 视频停止播放 回到初始状态
 */
var stop = function(video){
    video.pause();
    video.currentTime = 0;
}
/**
 * 内页 如何寄养 视频
 */
var videos = $('.how-video');
var resizeVideos = function(){
    videos.each(function(){
        var width = getPageWidth();
        if(width<=768){
            width = getPageWidth()*0.7;
        }else if(width<=992){
            width = getPageWidth()*0.8*0.7;
        }else{
            width = getPageWidth()*0.7*0.7;
        }
        $(this).css({
            width: width,
            height: width/810*560
        });
    });
}
///**
// * 视频自动循环播放
// */
//var videosRecyclePlay = function(){
//    for(var i = 0; i < videos.length; i ++){
//        videos[i]
//    }
//}
//$('.how-video-button').on('click',function(){
//    if($(this).hasClass('stop')){
//        var video = $(this).prev();
//        start(video[0]);
//        $(this).removeClass('stop');
//
//        video.on('click',function(){
//            stop($(this)[0]);
//            $(this).next().addClass('stop');
//            $(this).css({cursor: 'auto'});
//        })
//        video.css({cursor: 'pointer'});
//    }
//})
/**
 * 顶部导航背景变化
 */
var navbar = $('.navbar-default');
var navbarBgChange = function(){
    var toTop = document.body.scrollTop == 0?document.documentElement.scrollTop:document.body.scrollTop;
    var width = getPageWidth();
    var height;
    if(width <= 992){
        var topBannerHeight = width*(955/1920);//头部大背景宽高比
        topBanner.css('height', topBannerHeight);
        mainContent.css('top', topBannerHeight);
        height = topBannerHeight-30;
    }else{
        topBanner.css('height', 'auto');
        mainContent.css('top', '100vmin');
        height = getPageHeight()-70;
    }
    if(ifNavbarChange && toTop < height){
        navbar.removeClass('navbar-white-bg');
        navbar.find('.logo-top').attr('src',imgDir+'logo_black.png');
        navbar.find('.selected').css({backgroundColor: 'rgba(0,0,0,.3)'});
    }else if(ifNavbarChange && toTop >= height){
        navbar.addClass('navbar-white-bg');
        navbar.find('.logo-top').attr('src',imgDir+'logo_white.png');
        navbar.find('.selected').css({backgroundColor: 'transparent'});
    }
}
/**
 * 文字说明(带黑色透明背景)
 */
var contentFadeIn = function(){
    var height = getPageHeight();
    var toTop = document.body.scrollTop == 0?document.documentElement.scrollTop:document.body.scrollTop;
    if(fromBottom && fromBottom.length>0 && fromBottom.offset().top <= (height+toTop)){
        fromBottom.animate({
            bottom: 0,
            opacity: 1
        },1000);
    }
    if(fromLeft && fromLeft.length>0 && fromLeft.offset().top <= (height+toTop)){
        fromLeft.animate({
            left: 0,
            opacity: 1
        },1000);
    }
    if(fromRight && fromRight.length>0 && fromRight.offset().top <= (height*0.8+toTop)){
        //位于第一个
        fromRight.animate({
            right: 0,
            opacity: 1
        },1000);
    }
}
/**
 * 安全保障服务 图标变化
 */
var secIconsTrans = function(){
    var securityIcons = $('.security-icon').find('div.item-svg>svg');
    var black = '#55555D';
    var white = '#FFF';
    securityIcons.on('mouseover',function(){
        var toBlack = $(this).find('.svg-to-black');
        var toWhite = $(this).find('.svg-to-white');
        toWhite.css({'fill': white});
        toBlack.css({'fill': black});
    })
    securityIcons.on('mouseout',function(){
        var toBlack = $(this).find('.svg-to-black');
        var toWhite = $(this).find('.svg-to-white');
        toBlack.css({'fill': white});
        toWhite.css({'fill': black});
    })
}
/**
 * 帮助中心 高宽自适应
 */
var helpResize = function(){
    var width = getPageWidth(),height = 300,btnH = 50;
    var box = $('.help-box');
    var show = $('.help-box-show');
    var hide = show.next();
    var btn = hide.find('.btn');
    if(width <= 768){
        width *= 0.8;
        height = width/1.48138;
        if(width <= 600){
            btnH = height*0.0768;
        }else{
            btnH = height*0.144;
        }
    }else if(width <= 992){
        width *= 0.8*0.47;
        height = width/1.48138;
        btnH = height*0.0768;
    }else if(width <= 1400){
        width *= 0.85*0.45;
        height = width/1.48138;
        btnH = height*0.0768;
    }else if(width > 1400){
        width = 1400;
        width *= 0.85*0.45;
        height = width/1.48138;
        btnH = height*0.0768;
    }
    box.css({ height: height });
    show.css({ height: height });
    hide.css({
        height: height,
        marginTop: '-'+height+'px'
    });
    btn.css({ marginTop: btnH});
}
/**
 * 医院详情 图片滚动事件
 */
var detailSlide = function(){
    var width = getPageWidth();
    width = (width*0.8)/4;
    detailScroll.find('li').on('click',function(){
        var background = $(this)[0].style.background;
        detailBigImg.css({
            background: background+' no-repeat',
            '-webkit-background-size': 'cover',
            backgroundSize: 'cover'
        })
    });
    detailScroll.find('span.prev').on('click',function(){
        myScroll.scrollBy(width, 0, 500, IScroll.utils.ease.quadratic);
    });
    detailScroll.find('span.next').on('click',function(){
        myScroll.scrollBy(-width, 0, 500, IScroll.utils.ease.quadratic);
    });
}
/**
 * 医院详情 图片自适应
 */
var detailResize = function(){
    var width = getPageWidth();
    var height,sHeight,tHeight;
    var prev = detailScroll.find('span.prev');
    var next = detailScroll.find('span.next');
    var smallImgs = detailScroll.find('li');
    var wrapper = $('#wrapper');
    var scroller = $('#scroller');

    if(width <= 992){
        width = width*0.8;
        wrapper.css({ width: width-55 });
    }else{
        width = width*0.4166666667;
        wrapper.css({ width: width-70 });
    }
    height = width/1.33687429;
    sHeight = height/4.375;
    tHeight = height+19;
    detailBigImg.css({ height: height});
    prev.css({
        marginTop: tHeight,
        height: sHeight,
        paddingTop: sHeight/2-7
    });
    next.css({
        marginTop: tHeight,
        height: sHeight,
        paddingTop: sHeight/2-7
    });
    detailScroll.css({ height: sHeight });
    scroller.css({ width: (width/4)*smallImgs.length });
    smallImgs.css({
        width: (width-80)/4-5,
        height: sHeight
    });
}
/**
 * 页面响应变化
 */
$(function(){
    //头部导航背景
    navbarBgChange();
    //视频
    resizeVideos();
    //文字显示
    contentFadeIn();
})
$(window).on('scroll',function(){
    //首页
    navbarBgChange();
    //文字显示
    contentFadeIn();
})
$(window).on('resize',function(){
    //头部导航背景
    navbarBgChange();
    //视频
    resizeVideos();
})
